package org.resort.service.impl;

import org.resort.dao.*;
import org.resort.entity.*;
import org.resort.service.SuperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.Level;


/**
 * @Author 郭文虎
 * <p>
 * 超级管理员服务
 */
@Service("superService")
public class SuperServiceImpl implements SuperService {
    @Autowired
    PrivilegeDao privilegeDao;
    @Autowired
    RoleDao roleDao;
    @Autowired
    RolePrivilegeDao rolePrivilegeDao;
    @Autowired
    InDepartmentDao inDepartmentDao;
    @Autowired
    OutDepartmentDao outDepartmentDao;
    @Autowired
    MemberLevelDao memberLevelDao;
    @Autowired
    StateDao stateDao;
    @Autowired
    MemberDao memberDao;

    @Override
    public List<Privilege> getAllPrivilege() {
        return privilegeDao.getAllEntity();
    }

    @Override
    public int countAllPrivilege() {
        return privilegeDao.countAllEntity();
    }

    @Override
    public List<Privilege> getPartPrivilegeByPage(int offset, int limit) {
        return privilegeDao.getPartEntityByPage(offset, limit);
    }

    @Override
    public Privilege getOnePrivilege(int id) {
        return privilegeDao.getEntityById(id);
    }

    @Override
    public int changeOnePrivilegeById(int id, String name, String href, String icon, String parent) {
        Privilege privilege = null;
        if (parent != null && parent != "") {
            //将parent转换成int类型
            int p = Integer.parseInt(parent);
            //将属性装配成对象包括父权限
            Privilege privilegeParent = new Privilege(p);
            privilege = new Privilege(id, name, href, icon, privilegeParent);
        } else {
            privilege = new Privilege(id, name, href, icon);
        }
        //修改
        return privilegeDao.changeEntityById(privilege);
    }

    @Override
    public int addPrivilege(String name, String href, String icon, String parent) {
        Privilege privilege = null;
        if (parent != null && parent != "") {
            //将parent转换成int类型
            int p = Integer.parseInt(parent);
            //将属性装配成对象包括父权限
            Privilege privilegeParent = new Privilege(p);
            privilege = new Privilege(name, href, icon, privilegeParent);
        } else {
            //将属性装配成对象
            privilege = new Privilege(name, href, icon);
        }
        //添加
        return privilegeDao.addOneEntity(privilege);
    }

    @Override
    public int delPrivilegeById(int id) {
        //查询这个对象是否为父权限，如果是父权限那么将子权限也一并删除
        Privilege privilege = privilegeDao.getEntityById(id);
        if (privilege.getParent() == null) {
            //受影响行数
            int updates = 0;
            //获取到子权限
            List<Privilege> childrenPrivilegeList = privilegeDao.getPartEntityByParentId(id);
            //删除子权限
            for (Privilege childrenPrivilege : childrenPrivilegeList) {
                privilegeDao.delOneEntityById(childrenPrivilege.getId());
                updates++;
            }
            //删除父权限
            privilegeDao.delOneEntityById(id);
            updates++;
            return updates;
        }
        //这是子权限，直接删除即可
        else {
            return privilegeDao.delOneEntityById(id);
        }
    }

    @Override
    public List<Privilege> getAllParentPrivilege() {
        return privilegeDao.getAllParentPrivilege();
    }

    @Override
    public List<Privilege> getPartPrivilegeByPageAndParentId(int offset, int limit, int parentId) {
        //如果是全部那么分页获取所有
        if (parentId == 0) {
            return privilegeDao.getPartEntityByPage(offset, limit);
        }
        //根据父权限获取对应的子权限和当前父权限
        else {
            //获取对应的子权限
            List<Privilege> privilegeList = privilegeDao.getPartPrivilegeByPageAndParentId(offset, limit, parentId);
            //获取父权限
            privilegeList.add(0, privilegeDao.getEntityById(parentId));
            return privilegeList;
        }
    }

    @Override
    public int countPartPrivilegeByParentId(int parentId) {
        //如果是全部那么获取所有
        if (parentId == 0) {
            return privilegeDao.countAllEntity();
        }
        //根据父权限获取对应的子权限数量并加上一个父权限
        else {
            //对应的子权限数量
            int count = privilegeDao.countPartPrivilegeByParentId(parentId);
            //再加上一个父权限
            count++;
            return count;
        }
    }

    @Override
    public List<Role> getAllRole() {
        return roleDao.getAllEntity();
    }

    @Override
    public int countAllRole() {
        return roleDao.countAllEntity();
    }

    @Override
    public List<Role> getPartRoleByPage(int offset, int limit) {
        return roleDao.getPartEntityByPage(offset, limit);
    }

    @Override
    public List<Privilege> getPrivilegeByRoleId(int roleId) {
        //先获取关联表数据
        List<RolePrivilege> rolePrivilegeList = rolePrivilegeDao.getRolePrivilegeByRoleId(roleId);
        //解析出权限
        List<Privilege> privilegeList = new ArrayList<Privilege>();
        for (RolePrivilege rp : rolePrivilegeList) {
            privilegeList.add(rp.getPrivilege());
        }
        return privilegeList;
    }

    @Override
    public List<Privilege> getPartPrivilegeNotRoleId(int roleId) {
        //先获取关联表数据(所拥有的权限)
        List<RolePrivilege> rolePrivilegeList = rolePrivilegeDao.getRolePrivilegeByRoleId(roleId);
        //如果这名角色无已拥有权限，那么查出所有权限
        if (rolePrivilegeList == null || rolePrivilegeList.size() < 1) {
            return privilegeDao.getAllEntity();
        }
        //正常查询
        else {
            //所有未拥有权限的容器
            List<Privilege> privilegeList = new ArrayList<Privilege>();
            //查出所未拥有的父权限
            List<Integer> parentId = privilegeDao.getPartNotParentPrivilegeByRolePrivilege(rolePrivilegeList);
            List<Privilege> parentList = new ArrayList<Privilege>();
            for (Integer i : parentId) {
                if (i != null) {
                    privilegeList.add(privilegeDao.getEntityById(i));
                }
            }
            //查出所未拥有的子权限
            List<Privilege> childrenList = privilegeDao.getPartNotChildrenPrivilegeByRolePrivilege(rolePrivilegeList);
            for (Privilege privilege : childrenList) {
                privilegeList.add(privilege);
            }
            return privilegeList;
        }
    }

    @Override
    public int removePrivilege(int roleId, int privilegeId, String level) {
        /*
            如是将要删除的是父权限那么:
            1.查出对应的子权限
            2.子权限和父权限一起删除
         */
        if (level.equals("parent")) {
            int del = 0;
            List<Privilege> privilegeList = privilegeDao.getPartEntityByParentId(privilegeId);
            del += rolePrivilegeDao.removePrivilege(roleId, privilegeId);
            for (Privilege privilege : privilegeList) {
                del += rolePrivilegeDao.removePrivilege(roleId, privilege.getId());
            }
            return del;
        }
        //如果是子权限，那么直接删除(除非超级管理员要移除自己的权限分配)
        else {
            if (roleId == 1 && privilegeId == 37) {
                return 0;
            } else {
                return rolePrivilegeDao.removePrivilege(roleId, privilegeId);
            }
        }
    }

    @Override
    public int appendPrivilege(int privilegeId, int roleId, String level) {
        /*
            如果要添加的权限是父权限那么:
            1.查询对应的所有子权限
            2.删除RolePrivilege表中的对应子权限及父权限数据
            3.将父权限和对应子权限一并添加至RolePrivilege
         */
        if (level.equals("parent")) {
            List<Privilege> privilegeList = privilegeDao.getPartEntityByParentId(privilegeId);
            Privilege privilege = privilegeDao.getEntityById(privilegeId);
            privilegeList.add(privilege);
            int del = rolePrivilegeDao.delPartRolePrivilegeByRoleIdAndPrivilegeList(roleId, privilegeList);
            int add = 0;
            for (Privilege p : privilegeList) {
                add += rolePrivilegeDao.addOneRolePrivilegeByRoleIdAndPrivilegeId(roleId, p.getId());
            }
            return add;
        }
        /*
            如果将要添加的权限是子权限那么:
            1.查询子权限对应的父权限
            2.删除RolePrivilege表中的子权限和父权限数据(无论是否存在)
            3.将子权限和父权限添加
         */
        else {
            List<Privilege> privilegeList = new ArrayList<Privilege>();
            Privilege children = privilegeDao.getEntityById(privilegeId);
            Privilege parent = privilegeDao.getEntityById(children.getParent().getId());
            privilegeList.add(children);
            privilegeList.add(parent);
            int del = rolePrivilegeDao.delPartRolePrivilegeByRoleIdAndPrivilegeList(roleId, privilegeList);
            int add = 0;
            for (Privilege p : privilegeList) {
                add += rolePrivilegeDao.addOneRolePrivilegeByRoleIdAndPrivilegeId(roleId, p.getId());
            }
            return add;
        }
    }

    @Override
    public List<InDepartment> getAllInDepartment() {
        return inDepartmentDao.getAllEntity();
    }

    @Override
    public int addOneInDepartment(InDepartment inDepartment) {
        return inDepartmentDao.addOneEntity(inDepartment);
    }

    @Override
    public int changeOneInDepartment(InDepartment inDepartment) {
        return inDepartmentDao.changeEntityById(inDepartment);
    }

    @Override
    public InDepartment getOneInDepartmentById(int inDepartmentId) {
        return inDepartmentDao.getEntityById(inDepartmentId);
    }

    @Override
    public int delInDepartmentById(int inDepartmentId) {
        return inDepartmentDao.delOneEntityById(inDepartmentId);
    }

    @Override
    public List<OutDepartment> getAllOutDepartment() {
        return outDepartmentDao.getAllEntity();
    }

    @Override
    public int delOutDepartmentById(int outDepartmentId) {
        return outDepartmentDao.delOneEntityById(outDepartmentId);
    }

    @Override
    public List<State> getAllMemberState() {
        return stateDao.getAllMemberState();
    }

    @Override
    public List<MemberLevel> getAllMemberLevel() {
        return memberLevelDao.getAllEntity();
    }

    @Override
    public List<Member> getMemberByCondition(int stateId, int levelId, String name, int offset, int limit) {
        return memberDao.getPartMemberByCondition(stateId, levelId, name, offset, limit);
    }

    @Override
    public int countMemberByCondition(int stateId, int levelId, String name) {
        return memberDao.countMemberByCondition(stateId, levelId, name);
    }

    @Override
    public int addOneMember(Member member) {
        //设置账号
        member.setAccount(member.getPhone());
        //设置等级
        List<MemberLevel> memberLevelList = memberLevelDao.getAllEntityByPriceAsc();
        for (MemberLevel memberLevel : memberLevelList) {
            if (member.getBalance() >= memberLevel.getPrice()) {
                member.setMemberlevel(memberLevel);
            }
        }
        //设置状态
        List<State> stateList = stateDao.getAllMemberState();
        for (State state : stateList) {
            if (state.getName().equals("正常")) {
                member.setState(state);
            }
        }
        return memberDao.addOneMember(member);
    }

    @Override
    public Member getOneMemberById(int memberId) {
        return memberDao.getEntityById(memberId);
    }

    @Override
    public int updateMember(int id, String name, String identitycard, String account, double balance, String phone, int memberlevel, int state) {
        //装配member
        Member member = new Member();
        member.setId(id);
        member.setAccount(account);
        member.setIdentitycard(identitycard);
        member.setBalance(balance);
        member.setPhone(phone);
        member.setName(name);
        //装配memberlevel
        MemberLevel memberLevel = new MemberLevel();
        memberLevel.setId(memberlevel);
        member.setMemberlevel(memberLevel);
        //装配state
        State memberState = new State();
        memberState.setId(state);
        member.setState(memberState);
        return memberDao.changeEntityById(member);
    }

    @Override
    public int delOneMemberLevel(int memberLevelId) {
        return memberLevelDao.delOneEntityById(memberLevelId);
    }

    @Override
    public MemberLevel getOneMemberLevelById(int memberLevelId) {
        return memberLevelDao.getEntityById(memberLevelId);
    }

    @Override
    public int submitAddMemberLevel(MemberLevel memberLevel) {
        return memberLevelDao.addOneEntity(memberLevel);
    }

    @Override
    public int submitChangeMemberLevel(MemberLevel memberLevel) {
        return memberLevelDao.changeEntityById(memberLevel);
    }
}
